package com.hotel.tools;

import com.hotel.entity.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Str
 * @Date 2021/10/27 19:53
 * @Description :
 */
public class DBReader {

    // 获取用户列表
    public static List<User> getUserList() {
        String file = "D:\\Projects\\JavaWeb\\data\\users.txt";
        String line = null;
        BufferedReader br = null;
        ArrayList<User> list = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            list = new ArrayList<>();
            while ((line = br.readLine()) != null) {
//                User user = new User(line);
//                list.add(user);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert br != null;
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    // 根据用户名获取某个用户
    public static User getUser(String account) {
        String file = "D:\\Projects\\JavaWeb\\data\\users.txt";
        String line = null;
        BufferedReader br = null;
        User user = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            while ((line = br.readLine()) != null) {
//                user = new User(line);
                if (user.getAccount().equals(account)) {
                    return user;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert br != null;
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    // 判断是否存在某个账户名
    public static Boolean findUser(String account){
        String file = "D:\\Projects\\JavaWeb\\data\\users.txt";
        String line = null;
        BufferedReader br = null;
        User user = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            while ((line = br.readLine()) != null) {
//                user = new User(line);
                if (user.getAccount().equals(account)) {
                    return true;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert br != null;
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return false;
    }

}
